<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2019/12/18
 * Time: 15:25
 */
namespace App\Services\Backend\System;

use App\Common\Exception\ErrorException;
use App\Models\Backend\System\Dicitem;
use Illuminate\Support\Facades\Cache;

/**
 * 系统管理-数据定义管理 (服务)
 */
class DicService
{
    /**
     * @var \App\Repositories\Backend\System\DicRepository $dicRepository
     */
    private $dicRepository;
    /**
     * @var \App\Repositories\Backend\System\DicItemRepository $dicitemRepository
     */
    private $dicitemRepository;

    public function __construct()
    {
        $this->dicRepository = app('dicRepository');
        $this->dicitemRepository = app('dicitemRepository');
    }

    // 获取列表
    public function getList($requestData){
        $filters = [];
        // 搜索
        if(isset($requestData['filters']['dicname']) && !empty($requestData['filters']['dicname'])){
            $filters[] = ['dicname', 'like', '%'.$requestData['filters']['dicname'].'%'];
        }

        $sorts = ['id' => 'desc'];

        $result = $this->dicRepository->getList($filters,$sorts,$requestData['page']);

        return $result->toArray();
    }

    // 新增
    public function add($requestData){

        $insertid = $this->dicRepository->add($requestData);
        if ($insertid){
            Cache::forget($requestData['diccode']);
        }
        return $insertid;
    }

    //编辑
    public function edit($requestData){
        $id = $requestData['id'];
        $dic = $this->dicRepository->get($id);
        if(!$dic){
            throw new ErrorException('字典不存在');
        }
        $dic = $dic->toArray();
        $diccode = $dic['diccode'];
        if($diccode != $requestData['diccode']){
            $this->dicitemRepository->edit(['diccode'=>$requestData['diccode']], [['diccode', '=', $dic['diccode']]], FALSE);
        }
        Cache::forget($diccode);
        return $this->dicRepository->edit($requestData, $id);
    }

    //删除
    public function delete($requestData){
        $id = $requestData['id'];
        $dic = $this->dicRepository->get($id);
        if(!$dic){
            throw new ErrorException('字典不存在');
        }
        $dic = $dic->toArray();
        $this->dicRepository->delete($id);
        $this->dicitemRepository->delete([['diccode', '=', $dic['diccode']]], FALSE);
        Cache::forget($dic['diccode']);
    }

    // 获取条目列表
    public function getItemList($requestData){
        $filters = [];
        // 搜索
        if(isset($requestData['diccode']) && !empty($requestData['diccode'])){
            $filters[] = ['diccode', '=', $requestData['diccode']];
        }

        if(isset($requestData['filters']['itemcode']) && !empty($requestData['filters']['itemcode'])){
            $filters[] = ['itemcode', '=', $requestData['filters']['itemcode']];
        }

        if(isset($requestData['filters']['itemname']) && !empty($requestData['filters']['itemname'])){
            $filters[] = ['itemname', 'like', '%'.$requestData['filters']['itemname'].'%'];
        }

        $sorts = [
            'sortnum' => 'asc',
            'id' => 'asc'
        ];

        $result = $this->dicitemRepository->getList($filters,$sorts,$requestData['page'],['*'],$requestData['limit']);

        return $result->toArray();
    }

    public function getDicByCode($requestData)
    {
        $codes = $requestData['diccode'];
        if(empty($codes)) {
            throw new ErrorException('参数错误！');
        }

        $result = Dicitem::select('diccode', 'itemcode', 'itemname')->whereIn('diccode', $codes)->orderByDesc('sortnum')->get()->groupBy('diccode');

        return array_change_key_case($result->toArray(), CASE_LOWER);
    }

    // 获取字典条目所有列表
    public function getItemAll($filters=[], $sorts=[], $columns = ['*']){
        return $this->dicitemRepository->getAll($filters,$sorts,$columns);
    }

    // 新增条目
    public function addDicItem($requestData){

        $result = $this->dicitemRepository->add($requestData, TRUE);
        Cache::forget($result['diccode']);
        return $result;
    }

    //编辑条目
    public function editDicItem($requestData){
        $id = $requestData['id'];
        $dicitem = $this->dicitemRepository->get($id);
        $dicitem = $dicitem->toArray();
        if(empty($dicitem)){
            throw new ErrorException('定义项不存在');
        }
        $this->dicitemRepository->edit($requestData, $id);
        Cache::forget($dicitem['diccode']);
    }

    //删除条目
    public function deleteDicItem($requestData){
        $id = $requestData['id'];
        $dicitem = $this->dicitemRepository->get($id);
        $dicitem = $dicitem->toArray();
        if(empty($dicitem)){
            throw new ErrorException('定义项不存在');
        }
        $this->dicitemRepository->delete($id);
        Cache::forget($dicitem['diccode']);
    }

    public function getAll(){
        return $this->dicRepository->getAll([],['id' => 'asc']);
    }

    //*******************外部调用方法****************************************************
    public function getItemsCore($filters=[], $sorts =[], $columns=['*']){
        return $this->dicitemRepository->getAll($filters,$sorts,$columns);
    }
}
